package com.aiit.controller.admin;

import com.aiit.constant.JwtClaimsConstant;
import com.aiit.dto.EmployeeDTO;
import com.aiit.dto.EmployeeLoginDTO;
import com.aiit.dto.EmployeePageQueryDTO;
import com.aiit.entity.Employee;
import com.aiit.properties.JwtProperties;
import com.aiit.result.PageResult;
import com.aiit.result.Result;
import com.aiit.service.EmployeeService;
import com.aiit.utils.JwtUtil;
import com.aiit.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/shop")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("商家登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO, HttpServletRequest request) {
        log.info("商家登录：{}", employeeLoginDTO);
        String storedVerificationCode = (String) request.getSession().getAttribute("verificationCode");
        try {
            log.info("获取的 session: {}：", request.getSession());
            log.info("storedVerificationCode:", storedVerificationCode);

            if (storedVerificationCode == null) {
                log.warn("获取的 session: {}", request.getSession());
                return Result.error("验证码已过期或无效，请重新获取验证码。");
            }
            Employee employee = employeeService.login(employeeLoginDTO, storedVerificationCode);

            //登录成功后，生成jwt令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
            String token = JwtUtil.createJWT(
                    jwtProperties.getAdminSecretKey(),
                    jwtProperties.getAdminTtl(),
                    claims);
            log.info(jwtProperties.getAdminSecretKey());
            EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                    .id(employee.getId())
                    .userName(employee.getUsername())
                    .name(employee.getName())
                    .token(token)
                    .build();

            return Result.success(employeeLoginVO);
        } catch (Exception e) {
            log.error("商家登录失败：", e);
            return Result.error("登录失败：" + e.getMessage());
        }
    }

    /**
     * 员工分页查询
     *
     * @param employeePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("员工分页查询,参数为：{}", employeePageQueryDTO);
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }


    @PutMapping("/update")
    @ApiOperation("编辑员工信息")
    public Result Update(@RequestBody EmployeeDTO employeeDTO) {
        log.info("编辑员工信息:{}", employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }

    @PostMapping("/save")
    @ApiOperation("新增员工")
    public Result save(@RequestBody EmployeeDTO employeeDTO) {
        log.info("新增员工：{}", employeeDTO);
        System.out.println("当前线程的id:" + Thread.currentThread().getId());
        employeeService.save(employeeDTO);
        return Result.success();
    }


    @DeleteMapping("/delete")
    @ApiOperation("删除管理员")
    public Result delete(@RequestParam Long id) {
        log.info("删除管理员：{}", id);
        employeeService.deleteEmployee(id);
        return Result.success();
    }

}















